package com.huawei.codearts.controller;

import com.huawei.codearts.model.Member;
import com.huawei.codearts.service.EsApiService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 参考地址：
 * demo地址:https://blog.csdn.net/anjiongyi/article/details/123391835
 * 官方demo地址：https://github.com/elastic/elasticsearch-java/blob/8.1/java-client/src/test/java/co/elastic/clients/documentation/api_conventions/LoadingJsonTest.java
 */

@RestController
@RequestMapping("/es/api")
public class EsApiController {

    private final EsApiService esApiService;

    @Autowired
    public EsApiController(EsApiService esApiService) {
        this.esApiService = esApiService;
    }

    @RequestMapping("/create")
    public String create(@RequestParam Map<String, Object> params) {
        String result;
        try {
            Member member = esApiService.getCreateData(params);
            result = ResponseUtils.response(esApiService.create(member));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/update")
    public String update(@RequestParam Map<String, Object> params) {
        String result;
        try {
            Member member = esApiService.getUpdateData(params);
            result = ResponseUtils.response(esApiService.update(member));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/delDataVidId")
    public String delDataVidId(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String result;
        try {
            result = ResponseUtils.response(esApiService.delDataVidId(id));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/delDataVidQuery")
    public String delDataVidQuery(@RequestParam Map<String, Object> params) {
        String value = (String)params.get("value");
        String result;
        try {
            result = ResponseUtils.response(esApiService.delDataVidQuery(value));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/getId")
    public String getId(@RequestParam Map<String, Object> params) {
        String id = (String)params.get("id");
        String result;
        try {
            result = ResponseUtils.response(esApiService.getId(id));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping("/findDataQueryOne")
    public String findDataQueryOne(@RequestParam Map<String, Object> params) {
        String name = (String)params.get("name");
        String result;
        try {
            result = ResponseUtils.response(esApiService.findDataQueryOne("name", name));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    /**
     * 使用 elasticsearch-java
     */
    @RequestMapping("/search")
    public String search(@RequestParam Map<String, Object> params) {
        String name = (String)params.get("name");
        String result;
        try {
            result = ResponseUtils.response(esApiService.search(name));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    // 聚合查询
    @RequestMapping("/aggs")
    public String aggregations(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esApiService.aggregations());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    // 聚合模版查询
    @RequestMapping("/aggs/tpl")
    public String aggtpl(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esApiService.aggtpl());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    // 聚合模版查询 json版
    @RequestMapping("/aggs/json")
    public String aggjson(@RequestParam Map<String, Object> params) {
        String result;
        try {
            result = ResponseUtils.response(esApiService.aggjson());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

}
